<template>
    <!-- 选择是否更新的弹框组件 -->
    <div v-if="showUpdate">
        <div class="update-container drag"></div>
        <div class="update-panel no-drag">
            <div class="update-inner">
                <div class="update-content">
                    <div class="update-title">发现新版本 ({{ updateInfo.version }})</div>
                    <div class="update-list">
                        <div v-for="(item, index) in updateInfo.descriptionList" :key="index">
                            [{{ index + 1 }}] {{ item }}
                        </div>
                    </div>
                </div>
                <div class="download-progress" v-if="downloading">
                    <div v-if="downloadPercent.progress != 100">
                        <el-progress :percentage="downloadPercent.progress" />
                        <div class="download-tips">
                            正在下载，请稍候({{ proxy.Utils.size2Str(downloadPercent.loaded) }}/{{
                                proxy.Utils.size2Str(downloadPercent.total) }})
                        </div>
                    </div>
                    <div v-else>下载完成，准备安装</div>
                </div>
                <div class="op-btn" v-else>
                    <div class="cancel" @click="cancelUpdateHandler">残忍拒绝</div>
                    <div class="update" @click="startDownload">更新</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, nextTick, watch, onMounted, onUnmounted } from "vue"
import { useRoute, useRouter } from "vue-router";
const { proxy } = getCurrentInstance();
const route = useRoute();
const router = useRouter();

import config from "../../../../package.json";
import { useUserInfoStore } from "@/stores/userInfoStore";
const userInfoStore = useUserInfoStore();

const props = defineProps({
    autoUpdate: {
        type: Boolean,
        default: true
    }
})
const showUpdate = ref(false);
const updateInfo = ref({});
const downloading = ref(false);
const downloadPercent = ref({
    progress: 0,
    loaded: 0,
    total: updateInfo.value.size
});

const cancelUpdateHandler = () => {
    showUpdate.value = false;
};

const checkUpdate = async (isAuto) => {
    let result = await proxy.Request({
        url: proxy.Api.checkVersion,
        params: {
            version: config.version
        }
    })
    if (!result) {
        return;
    }
    if (result.data == null) {
        if (!isAuto) {
            // 是用户手动检查更新的，需要提示用户已经是最新版本
            proxy.Confirm({
                message: "当前已经是最新版本，无需更新",
                showCancelBtn: false
            })
        }
        return;
    }
    showUpdate.value = true;
    updateInfo.value = result.data;
}

defineExpose({
    checkUpdate
})

const startDownload = () => {
    switch (updateInfo.value.methodType) {
        case "FILE":
            downloading.value = true;
            myApi.downloadUpdate({
                version: updateInfo.value.version,
                fileName: updateInfo.value.fileName,
            })
            break;
        case "OUTER_LINK":
            myApi.openUrl(updateInfo.value.outerLink)
            break;
    }
};

/* 监听下载进度，实时更新 */
myApi.onUpdateDownloadProgress((event, loaded) => {
    downloadPercent.value.loaded = loaded;
    downloadPercent.value.progress = Math.floor(loaded / updateInfo.value.size * 100);
})

onMounted(() => {
    if (props.autoUpdate) {
        checkUpdate(true);
    }
})
onUnmounted(() => {
    myApi.removeAllListeners("updateDownloadProgress");
})
</script>

<style lang="scss" scoped>
.update-container {
    opacity: 0.2;
    background: #000;
    z-index: 1;
    position: absolute;
    left: 0px;
    top: 0px;
    width: 100%;
    height: calc(100vh);
}

.update-panel {
    top: 100px;
    left: 0px;
    width: 100%;
    position: absolute;
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;

    .update-inner {
        background-image: url('@/assets/img/update.png');
        background-size: 100%;
        background-position: top center;
        background-repeat: no-repeat;
        width: 350px;
        min-height: 400px;

        .update-content {
            margin-top: 230px;
            background: #fff;
            padding: 15px;

            .update-title {
                font-size: 20px;
                color: #3e84c5;
            }

            .update-list {
                margin-top: 5px;
                max-height: 150px;
                overflow: auto;
            }
        }

        .download-progress {
            background: #fff;
            padding: 10px;
            border-radius: 0px 0px 10px 10px;

            .download-tips {
                margin-top: 5px;
                text-align: center;
                font-size: 14px;
                color: #6e6e6e;
            }
        }

        .op-btn {
            background: #fff;
            border-radius: 0px 0px 10px 10px;
            border-top: 1px solid#ddd;
            display: flex;
            align-items: center;
            overflow: hidden;
            line-height: 40px;

            .cancel {
                width: 50%;
                text-align: center;
                color: #989898;
                cursor: pointer;
            }

            .update {
                width: 50%;
                border-left: 1px solid#ddd;
                text-align: center;
                background: #08a0e7;
                color: #fff;
                cursor: pointer;
            }
        }
    }
}
</style>
